package art.shared.entities;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;

public class ArtShared implements IsSerializable {

	private long id;
	private GenreShared genre;
	// private File file;
	private long creatorId;
	private String title;
	private Date uploadedOn;
	private List<RatingShared> ratings;

	public ArtShared() {
		ratings = new ArrayList<RatingShared>();
	}

	public ArtShared(GenreShared genre, long creator, String title,
			Date uploadedOn) {
		this();
		this.genre = genre;
		this.creatorId = creator;
		this.title = title;
		this.uploadedOn = uploadedOn;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public Double getAvgRating() {
		int count = 0;
		int sum = 0;
		for (RatingShared rating : getRatings()) {
			if (rating.getArtId() == getId()) {
				Integer rate = rating.getRate();
				if (rate != null) {
					count++;
					sum += rate;
				}
			}
		}
		if (count == 0) {
			return 0.0;
		}
		return (double)sum / (double)count;
	}

	public GenreShared getGenre() {
		return genre;
	}

	public void setGenre(GenreShared genreShared) {
		this.genre = genreShared;
	}

	public long getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(long creator) {
		this.creatorId = creator;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getUploadedOn() {
		return uploadedOn;
	}

	public void setUploadedOn(Date uploadedOn) {
		this.uploadedOn = uploadedOn;
	}

	public List<RatingShared> getRatings() {
		return ratings;
	}

	public void setRatings(List<RatingShared> ratings) {
		this.ratings = ratings;
	}
	
	public void addRating(RatingShared rating) {
		ratings.add(rating);
	}

}